Code
joined_vacomparison <- joined_vacomparison %>%
mutate(
dem_dif = round_half_up((pct_mcauliffe - biden_pct), 2),
rep_dif = round_half_up((pct_youngkin - trump_pct), 2)
) Taylor Atkins
This project is similar to the interactive county map work because they both are based around maps using the tidyverse package. To begin we load up the packages that are going to be used as well as the data which was formulated during class.
This project will be using data to compare the most recent gubernatorial and presidential elections in Virginia. To begin we calculate the difference between the gubernatorial results and presidential results for democrat and republican candidates.
We then calculate the election results difference and incorporate that with the general election data we began with.
# A tibble: 6 × 11
locality dem_dif rep_dif biden trump biden…¹ trump…² young…³ mcaul…⁴ pct_y…⁵
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int> <dbl>
1 ACCOMACK … -6.31 7.01 7578 9172 44.7 54.1 7878 4948 61.1
2 ALBEMARLE… -3.63 5.03 42466 20804 65.7 32.2 19141 31919 37.2
3 ALEXANDRI… -5.08 6.39 66240 14544 80.3 17.6 14013 43866 24.0
4 ALLEGHANY… -2.37 3.09 2243 5859 27.3 71.4 4530 1518 74.5
5 AMELIA CO… -5.13 5.9 2411 5390 30.6 68.3 4720 1617 74.2
6 AMHERST C… -4.92 6.07 5672 11041 33.4 64.9 9731 3897 71
# … with 1 more variable: pct_mcauliffe <dbl>, and abbreviated variable names
# ¹biden_pct, ²trump_pct, ³youngkin, ⁴mcauliffe, ⁵pct_youngkin
# A tibble: 6 × 11
locality dem_dif rep_dif biden trump biden…¹ trump…² young…³ mcaul…⁴ pct_y…⁵
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int> <dbl>
1 WESTMOREL… -6.32 7.01 4501 5318 45.3 53.5 4614 2971 60.6
2 WILLIAMSB… -5 6.02 4790 1963 69.6 28.5 1703 3185 34.5
3 WINCHESTE… -4.06 5.56 6610 5221 54.6 43.1 4137 4294 48.7
4 WISE COUN… -3.17 3.45 3110 13366 18.7 80.4 9691 1796 83.9
5 WYTHE COU… -3.18 3.93 3143 11733 20.8 77.8 9458 2043 81.8
6 YORK COUN… -4.74 6.4 17683 20241 45.6 52.2 17485 12190 58.6
# … with 1 more variable: pct_mcauliffe <dbl>, and abbreviated variable names
# ¹biden_pct, ²trump_pct, ³youngkin, ⁴mcauliffe, ⁵pct_youngkin
Above, we select the variables that will be used in the map later. Below, we gather all the data that is needed to produce a map in R.
This removes the “M” at the end of some variables that were automatically pulled from the census.
This removes the “E” at the end of some variables that were automatically pulled from the census.
Changes the name of one of the columns of “locality”.
Then, we clean up some of the data on the back-end.
Finally we begin to map Virginia and its counties using mapview.
Next, we turn off legends, hover text, popups.
Warning: Found less unique colors (9) than unique zcol values (116)!
Interpolating color vector to match number of zcol values.
Here we create and insert custom labels.
Warning: Found less unique colors (9) than unique zcol values (116)!
Interpolating color vector to match number of zcol values.
Next we customize the pop-ups for the counties.
mypopup <- glue::glue("<strong>{va_counties_data$locality}</strong><br />
Total Population: {va_counties_data$totalpop}<br />
Difference in D Elections: {va_counties_data$dem_dif}<br />
Median Income: ${va_counties_data$medincome}<br />
Median Age: {va_counties_data$medage}") %>%
lapply(htmltools::HTML)
# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>%
# lapply(htmltools::HTML)[[1]]
<strong>LEE COUNTY, VIRGINIA</strong><br />
Total Population: 22482<br />
Difference in D Elections: -2.84<br />
Median Income: $37574<br />
Median Age: 45.6
[[2]]
<strong>ROCKBRIDGE COUNTY, VIRGINIA</strong><br />
Total Population: 22663<br />
Difference in D Elections: -2.38<br />
Median Income: $57828<br />
Median Age: 49.2
[[3]]
<strong>ACCOMACK COUNTY, VIRGINIA</strong><br />
Total Population: 33388<br />
Difference in D Elections: -6.31<br />
Median Income: $50601<br />
Median Age: 47.1
[[4]]
<strong>KING GEORGE COUNTY, VIRGINIA</strong><br />
Total Population: 26597<br />
Difference in D Elections: -6.99<br />
Median Income: $101599<br />
Median Age: 38.3
[[5]]
<strong>TAZEWELL COUNTY, VIRGINIA</strong><br />
Total Population: 40615<br />
Difference in D Elections: -2.83<br />
Median Income: $42937<br />
Median Age: 45.1
[[6]]
<strong>BUCKINGHAM COUNTY, VIRGINIA</strong><br />
Total Population: 16832<br />
Difference in D Elections: -6.6<br />
Median Income: $49841<br />
Median Age: 43
Then we incorporate those pop-ups.
Warning: Found less unique colors (9) than unique zcol values (116)!
Interpolating color vector to match number of zcol values.
This project incorporates work from two separate projects worked on in/for class to create our own new piece. In my case we are able to see the difference in votes for the Democratic party between the 2020 presidential election and the 2022 gubernatorial races while comparing that to the median income and median age of the counties. There is certainly some interesting information that can be pulled from this, but the biggest take away for me was that there isn’t a single counties in Virginia where the democratic governor got a higher percentage of the vote than President Biden did. Perhaps this is saying something about the political leanings of the state or perhaps the election turnout from Democrats for non-presidential races.
---
title: "Virginia Election Project"
author: "Taylor Atkins"
execute:
echo: true
format:
html:
self-contained: true
code-fold: true
code-tools: true
---
```{r setup, include=FALSE, warning=FALSE, message=FALSE}
library(tidyverse)
library(janitor)
library(kableExtra)
library(here)
options(scipen = 999)
options(stringsAsFactors = FALSE)
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tigris)
library(sf)
library(tidycensus)
library(htmltools)
library(janitor)
library(here)
library(mapview)
library(leafsync)
library(leaflet.extras2)
options(tigris_class = "sf")
#load saved joined data file from previous script
joined_vacomparison <- readRDS(here("processed_data", "joined_vacomparison.rds"))
```
This project is similar to the interactive county map work because they both are based around maps using the tidyverse package. To begin we load up the packages that are going to be used as well as the data which was formulated during class.
# Comparing Virgnia Gov vs. Prez Races
This project will be using data to compare the most recent gubernatorial and presidential elections in Virginia. To begin we calculate the difference between the gubernatorial results and presidential results for democrat and republican candidates.
```{r}
joined_vacomparison <- joined_vacomparison %>%
mutate(
dem_dif = round_half_up((pct_mcauliffe - biden_pct), 2),
rep_dif = round_half_up((pct_youngkin - trump_pct), 2)
)
```
```{r, echo = FALSE}
#joined_vacomparison <- joined_vacomparison %>%
#arrange(dem_dif)
```
We then calculate the election results difference and incorporate that with the general election data we began with.
```{r}
joined_vacomparison <- joined_vacomparison %>%
relocate(dem_dif, rep_dif, .after = locality)
head(joined_vacomparison)
tail(joined_vacomparison)
```
```{r, echo = FALSE}
# uncomment to run, then recomment it out so you don't run it every time
#census_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", install=TRUE)
```
```{r}
#chose variables we want
myvars <- c(totalpop = "B01003_001",
medincome = "B19013_001",
medage = "B01002_001"
)
```
Above, we select the variables that will be used in the map later. Below, we gather all the data that is needed to produce a map in R.
```{r, results = FALSE, message = FALSE}
#pull for VA counties
va_counties_withgeo <- get_acs(geography = "county",
variables = c(myvars),
state = "VA",
output = "wide",
geometry = TRUE)
va_counties_withgeo
```
```{r, results = FALSE, message = FALSE}
#all counties in the US
all_counties_withgeo <- get_acs(geography = "county",
variables = c(myvars),
output = "wide",
geometry = TRUE)
all_counties_withgeo
```
This removes the "M" at the end of some variables that were automatically pulled from the census.
```{r, results = FALSE}
#remove MOE columns - they all end with "M"
va_counties_withgeo <- va_counties_withgeo %>%
select(-ends_with("M"))
va_counties_withgeo
```
This removes the "E" at the end of some variables that were automatically pulled from the census.
```{r, results = FALSE}
#remove that trailing "E"
colnames(va_counties_withgeo) <- sub("E$", "", colnames(va_counties_withgeo)) # $ means end of string only
va_counties_withgeo
```
Changes the name of one of the columns of "locality".
```{r}
va_counties_withgeo <- va_counties_withgeo %>%
mutate(NAM = str_to_upper(NAM))
```
```{r}
va_counties_withgeo <- va_counties_withgeo %>%
rename("locality" = "NAM")
```
Then, we clean up some of the data on the back-end.
```{r}
joined_vacomparison <- joined_vacomparison %>%
mutate(locality = paste(locality, ", VIRGINIA", sep = ""))
```
```{r, message = FALSE}
va_counties_data <- full_join(va_counties_withgeo, joined_vacomparison)
```
Finally we begin to map Virginia and its counties using mapview.
```{r}
mapview(va_counties_data, zcol = "dem_dif")
```
Next, we turn off legends, hover text, popups.
```{r}
mapview(va_counties_data, zcol = "dem_dif",
col.regions = RColorBrewer::brewer.pal(9, "Blues"),
alpha.regions = 1,
legend = FALSE,
label = FALSE,
popup = FALSE)
```
Here we create and insert custom labels.
```{r}
mylabel <- glue::glue("{va_counties_data$locality} {va_counties_data$dem_dif}")
mapview(va_counties_data, zcol = "dem_dif",
col.regions = RColorBrewer::brewer.pal(9, "Blues"),
alpha.regions = 1,
label = mylabel)
```
Next we customize the pop-ups for the counties.
```{r}
mypopup <- glue::glue("<strong>{va_counties_data$locality}</strong><br />
Total Population: {va_counties_data$totalpop}<br />
Difference in D Elections: {va_counties_data$dem_dif}<br />
Median Income: ${va_counties_data$medincome}<br />
Median Age: {va_counties_data$medage}") %>%
lapply(htmltools::HTML)
# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>%
# lapply(htmltools::HTML)
```
```{r}
head(mypopup)
```
Then we incorporate those pop-ups.
```{r}
mapview(va_counties_data, zcol = "dem_dif",
col.regions = RColorBrewer::brewer.pal(9, "Blues"),
alpha.regions = 1,
popup = mypopup,
label = mylabel)
```
This project incorporates work from two separate projects worked on in/for class to create our own new piece. In my case we are able to see the difference in votes for the Democratic party between the 2020 presidential election and the 2022 gubernatorial races while comparing that to the median income and median age of the counties. There is certainly some interesting information that can be pulled from this, but the biggest take away for me was that there isn't a single counties in Virginia where the democratic governor got a higher percentage of the vote than President Biden did. Perhaps this is saying something about the political leanings of the state or perhaps the election turnout from Democrats for non-presidential races.